/*
 * 显示图像管理器侧栏中计算机目录
 */
package com.propic.www;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Effect;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;

public class Filebrower extends Application{

	public static int flag = 0;
	public static List<ImageView> imageViews = new ArrayList<>();
	public static int border = 30;
	public static StackPane leftpane = new StackPane();
	public static StackPane rightpane = new StackPane();

	public static boolean showEnd = false;
	public static int showEndnum = 0;
	
	public static TreeView<File> buildFileSystemBrowser(String path) {
	      TreeItem<File> root = createNode(new File(path));
	      TreeView<File> filetree = new TreeView<File>(root);
	      filetree.setCellFactory(new Callback<TreeView<File>, TreeCell<File>>() {
			@Override
			public TreeCell<File> call(TreeView<File> paramT) {
				// TODO Auto-generated method stub
				TreeCell<File> treeCell =  new TreeCell<File>(){
					protected void updateItem(File paramT, boolean paramBoolean) {
						super.updateItem(paramT, paramBoolean);
						if(!isEmpty()){
							Text texttmp = new Text(paramT.getName());
							texttmp.setFill(Color.BLUEVIOLET);
							setGraphic(texttmp);

							final TreeCell<File> this$ = this;
							this$.applyCss();

							File tmp = this$.getItem();
							
							//为Treecell绑定动作，如果目录下存在图片就加入showAll和opAll两个menuitem，如果目录下没有这两项就不显示
							this.setOnMouseClicked(t->{
								MouseButton button = t.getButton();
								if(HasImage.hasImage(tmp) && button==MouseButton.SECONDARY){
									Mainwindows.opAll.setVisible(true);
									Mainwindows.showAll.setVisible(true);
									Mainwindows.showSigle.setVisible(true);
								}else {
									Mainwindows.opAll.setVisible(false);
									Mainwindows.showAll.setVisible(false);
									Mainwindows.showSigle.setVisible(false);
								}
								/*
								如果复制文件列表为空，则不显示复制文件这一选项
								 */
								if(Mainwindows.copyfile.isEmpty()){
									Mainwindows.cpAll.setVisible(false);
								}else {
									Mainwindows.cpAll.setVisible(true);
								}
								
								switch (button) {
                                    case NONE:
                                        break;
                                    case PRIMARY:
										if (t.getClickCount()==1){
											Mainwindows.copyfile.clear();
										}else if (t.getClickCount() == 2){
											String pathnow = tmp.getAbsolutePath();
											this.getChildren().clear();
											this.getChildren().add(new Filebrower().buildFileSystemBrowser(pathnow));
											System.out.println("清理干净");

										}
                                        break;
                                    case MIDDLE:
                                        break;
                                    case SECONDARY:
									if(t.getClickCount()==1){
										Mainwindows.menuTree.show(this$, t.getScreenX(), t.getScreenY());
										//为opAll绑定动作
										Mainwindows.opAll.setOnAction(e->{
											try {
												Mainwindows.pathtmp = tmp.getAbsolutePath();
												new ShowFolderPic().showAllPic(tmp.getAbsolutePath());
												Mainwindows.copyfile.clear();
											} catch (Exception e1) {
												e1.printStackTrace();
											}
										});
//										System.out.println(tmp.getAbsolutePath());
										Mainwindows.cpAll.setOnAction(e->{
											String Aimname = tmp.getAbsolutePath();
											Copy.copyOne(Mainwindows.copyfile, Aimname);
										});
										
										//为showAll绑定动作
										Mainwindows.showAll.setOnAction(e->{
											SimpleSlideShow simpleSlideShow = new SimpleSlideShow();
											String AimnameforShow = tmp.getAbsolutePath();
											simpleSlideShow.setAll(AimnameforShow);
											StackPane tmppane = simpleSlideShow.getRoot();
											Stage tmpstage = new Stage();
											Scene tmpscene = new Scene(tmppane, 1000, 600);
											tmpstage.setScene(tmpscene);
											tmpstage.show();
											simpleSlideShow.start();
											
										});

										
									}
									break;

								default:
									break;
								}
								
							});
						}
					}
				};
                return treeCell;
			}
		});

        return filetree;
	  }
//	  private class Treeplus extends TreeCell<File>{
//		  private final ContextMenu showimg = new ContextMenu();
//		  public Treeplus(){
//			  MenuItem showpic = new MenuItem("显示目录下的图片");
//			  showimg.getItems().add(showpic);
//			  showpic.setOnAction(e->{
//				  File shownext = getTreeItem().getValue();
//				  if(shownext != null && shownext.isDirectory()){
//					  try {
//						  Mainwindows.centerpane.getChildren().add(new ShowFolderPic().showAllPic(shownext.getAbsolutePath()));
//					  } catch (Exception e1) {
//						  e1.printStackTrace();
//					  }
//				  }
//			  });
//		  }
//	  }

	  protected static TreeItem<File> createNode(final File f) {
	      return new TreeItem<File>(f) {

	          private boolean isLeaf;

	          private boolean isFirstTimeChildren = true;
	          private boolean isFirstTimeLeaf = true;

	          @Override
	          public ObservableList<TreeItem<File>> getChildren() {
	              if (isFirstTimeChildren) {
	                  isFirstTimeChildren = false;

	                  super.getChildren().setAll(buildChildren(this));
	              }
	              return super.getChildren();
	          }

	          @Override
	          public boolean isLeaf() {
	              if (isFirstTimeLeaf) {
	                  isFirstTimeLeaf = false;
	                  File f = (File) getValue();
	                  isLeaf = new Isimage().isImage(f);
	              }

	              return isLeaf;
	          }
	          public ObservableList<TreeItem<File>> buildChildren(TreeItem<File> TreeItem00) {
	              File f = TreeItem00.getValue();
	              TreeItem00.setExpanded(true);

	              if (f != null && f.isDirectory() ) {
	                  File[] files = f.listFiles();
	                  if (files != null) {
	                      ObservableList<TreeItem<File>> children = FXCollections.observableArrayList();

	                      for (File childFile : files) {
	                          children.add(createNode(childFile));

	                      }

	                      return children;
	                  }
	              }

	              return FXCollections.emptyObservableList();
	          }
	      };
	  }



	@Override
	public void start(Stage arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
